import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var Viewer: any;
@Component({
	selector: 'sa-project-company',
	templateUrl: './project-company.component.html',
	styleUrls: ['./project-company.css']
})
export class ProjectCompanyComponent implements OnInit {
	tableData: any[] = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	tableParam: any = {
		searchKey: '',
		startTime: '',
		endTime: '',
	}
	isViewer: boolean = false; //默认隐藏
	isShowDelete = true; // 默认不显示删除按钮
	title: any = "";  // 展示回复的标题
	content: any = "";  // 展示回复的内容

	// 添加公司的数据
	selectedCompanyName: any = "";
	selectedCompanyId: any = "";
	signUnit: any = ""; // 签约单位
	isRfid: any = false; //  是否是带RFID的公司（0不是 1是）

	validatorOption = {
		excluded: [":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			selectedCompanyName: {
				trigger: "change",
				validators: {
					notEmpty: {
						message: '请选择公司'
					}
				}
			},
			// selectedCompanyId: {
			// 	validators: {
			// 		notEmpty: {
			// 			message: '请选择公司'
			// 		}
			// 	}
			// }
		}
	}
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}
	ngOnInit() {
		this.initBtn();
		this.load();
	}
	//初始化权限按钮
	initBtn() {
		// 公司的删除按钮的权限
		// var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_wx:bind:adviseLog')));
		// if(!btnData) {
		// 	return
		// };
		// for(let item of btnData) {
		// 	if(item.sourceKey === "wx-bind:advise-log:deleteBtn" && item.parentId) {
		// 		this.isShowDelete = true;
		// 	}
		// }
	}
	//table刷新
	refresh() {
		this.load();
	}
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.tableParam.startTime = time.startTime;
		this.tableParam.endTime = time.endTime;
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				searchKey: common.stringTrim(this.tableParam.searchKey),
				startTime: this.tableParam.startTime,
				endTime: this.tableParam.endTime,
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("projectCompany/projectCompanyList.html", reqdata).subscribe(result => {
			if (result.isSuccess && result.exhibitDatas) {
				this.tableData = result.exhibitDatas
				this.totalCount = result.recordCount;
			}
		});
	}
	// 添加公司弹框
	addCompany(modal) {
		this.selectedCompanyName = ""
		this.selectedCompanyId = ""
		this.isRfid = false
		this.signUnit = ""
		$("#addCompanyFrom").data('bootstrapValidator').resetForm();
		modal.show();
	}
	// 是否开启周期设置
	isOpenCycleClick() {
		this.isRfid = !this.isRfid;
	}
	// 公司选中回调
	factorySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
	// 添加公司提交
	itemModalSubmit(modal) {
		$('#addCompanyFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#addCompanyFrom').data('bootstrapValidator').isValid();  //验证是否通过
		let reqData = {
			companyId: this.selectedCompanyId,
			companyName: this.selectedCompanyName,
			isRfid: this.isRfid ? 1 : 0,
			signUnit: this.signUnit
		}
		if (isValid) {
			this.jsonApiService.http_obj("projectCompany/addProjectCompany.html", reqData).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
				this.itemModalClose(modal)
				// if (data.success) {
				// }
			})
		}
	}
	// 添加公司弹框关闭
	itemModalClose(modal) {
		modal.hide();
		$("#addCompanyFrom").data('bootstrapValidator').resetForm();
	}
	// 删除登录日志
	deleteRow(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let parameters = {
				id: row.id,
			}
			this.jsonApiService.http_obj("projectCompany/delProjectCompany.html", parameters).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}



	showMessage(content, title, modal) {
		modal.show();
		this.title = title
		this.content = content
	}
}